
package com.carkeeper.servcieManagement.controller;

import com.carkeeper.common.utils.PageResult;
import com.carkeeper.common.utils.Result;
import com.carkeeper.common.utils.StatusCode;
import com.carkeeper.servcieManagement.service.ServiceTypeService;
import com.carkeeper.serviceManagement.pojo.ServiceType;
import com.github.pagehelper.Page;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.web.bind.annotation.*;

import java.util.List;
import java.util.Map;


/**
 * @author pengjinxiong
 * @version 1.0
 * @date 2020/11/23 14:49
 */

@RestController
@RequestMapping("/serviceType")
public class ServiceTypeController {
    @Autowired
    private ServiceTypeService serviceTypeService;


    /**
     * 查询全部数据
     *
     * @return
     */

    @GetMapping
    public Result findAll() {
        List<ServiceType> serviceTypeList = serviceTypeService.findAll();
        return new Result(true, StatusCode.OK, "查询成功", serviceTypeList);
    }


    /***
     * 根据ID查询数据
     * @param id
     * @return
     */

    @GetMapping("/{id}")
    public Result findById(@PathVariable("id") Long id) {
        ServiceType serviceType = serviceTypeService.findById(id);
        return new Result(true, StatusCode.OK, "查询成功", serviceType);
    }


    /***
     * 新增数据
     * @param serviceType
     * @return
     */

    @PostMapping
    public Result add(@RequestBody ServiceType serviceType) {
        serviceTypeService.add(serviceType);
        return new Result(true, StatusCode.OK, "添加成功");
    }


    /***
     * 修改数据
     * @param serviceType
     * @param id
     * @return
     */

    @PutMapping("/{id}")
    public Result update(@RequestBody ServiceType serviceType, @PathVariable Long id) {
        serviceType.setId(id);
        serviceTypeService.update(serviceType);
        return new Result(true, StatusCode.OK, "修改成功");
    }

    /***
     * 分页搜索实现
     * @param searchMap
     * @param page
     * @param size
     * @return
     */

    @GetMapping(value = "/search/{page}/{size}")
    public Result findPage(@RequestParam Map searchMap, @PathVariable int page, @PathVariable int size) {
        Page<ServiceType> pageList = serviceTypeService.findPage(searchMap, page, size);
        PageResult pageResult = new PageResult(pageList.getTotal(), pageList.getResult());
        return new Result(true, StatusCode.OK, "查询成功", pageResult);
    }

    @DeleteMapping("{id}")
    public Result delete(@PathVariable("id") Long id){
      int i=serviceTypeService.delete(id);
      if(i==0){
          return  new Result(false,StatusCode.ERROR,"修改失败");
      }
        return new Result(true, StatusCode.OK, "修改成功");
    }

    //查询所有一级分类
    @GetMapping("/findLevel1")
    public Result<List<ServiceType>> findLevel1(){
        List<ServiceType> list=serviceTypeService.findLevel1();
        return new Result(true,StatusCode.OK,"查询成功",list);
    }


    //根据一级菜单id查询所有二级菜单
    @GetMapping("/findLevel2/{id}")
    Result<List<ServiceType>> findLevel2(@PathVariable("id") Long id){
        List<ServiceType> list=serviceTypeService.findLevel2(id);
        return new Result<>(true,StatusCode.OK,"查询成功",list);
    }










}

